home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 12581 / 12581.xpi / chrome / mintrayr.jar / content / songbird / player.js next >
Text File  |  2010-01-21  |  7KB  |  209 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is MiniTrayR extension
  15.  *
  16.  * The Initial Developer of the Original Code is
  17.  * Nils Maier.
  18.  * Portions created by the Initial Developer are Copyright (C) 2008
  19.  * the Initial Developer. All Rights Reserved.
  20.  *
  21.  * Contributor(s):
  22.  *   Nils Maier <MaierMan@web.de>
  23.  *
  24.  * Alternatively, the contents of this file may be used under the terms of
  25.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  26.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27.  * in which case the provisions of the GPL or the LGPL are applicable instead
  28.  * of those above. If you wish to allow use of your version of this file only
  29.  * under the terms of either the GPL or the LGPL, and not to allow others to
  30.  * use your version of this file under the terms of the MPL, indicate your
  31.  * decision by deleting the provisions above and replace them with the notice
  32.  * and other provisions required by the GPL or the LGPL. If you do not delete
  33.  * the provisions above, a recipient may use your version of this file under
  34.  * the terms of any one of the MPL, the GPL or the LGPL.
  35.  *
  36.  * ***** END LICENSE BLOCK ***** */
  37.  
  38. const MinTrayR_CallbackTimer = Components.Constructor('@mozilla.org/timer;1', 'nsITimer', 'initWithCallback');
  39.  
  40. function MinTrayRSongbird() {
  41.     MinTrayR.call(this, 'MinTrayR_context', 'songbird.watchplayer');
  42.     var nodes = [
  43.       this.cloneToMenu('MinTrayR_sep-top', ['menuitem_control_play', 'menuitem_control_next', 'menuitem_control_prev'], false),
  44.         this.cloneToMenu('MinTrayR_sep-middle', ['menuitem_control_repx', 'menuitem_control_repa', 'menuitem_control_rep1'], false),
  45.         this.cloneToMenu('MinTrayR_sep-bottom', ['menu_FileQuitItem'], true)
  46.     ];
  47.     
  48.     addEventListener(
  49.         'TrayMinimize',
  50.         function(evt) gMinTrayR && (gMinTrayR._mayFloat = false),
  51.         true
  52.     );
  53.     addEventListener(
  54.         'TrayRestore',
  55.         function(evt) gMinTrayR && (gMinTrayR._mayFloat = true),
  56.         true
  57.     );
  58.     addEventListener(
  59.         'TrayClick',
  60.         function(evt) {
  61.             if (evt.button == 1 && gMinTrayR && gMinTrayR.prefs.getExt('songbird.middleclickplays')) {
  62.                 doMenu('menuitem_control_play');
  63.             }
  64.         },
  65.         true
  66.     );
  67.     
  68.     
  69.     for each (let n in nodes) {
  70.         for each (let e in n) {
  71.             e.setAttribute('oncommand', 'doMenu(event.target.getAttribute("mintrayr_origid"))');
  72.         }
  73.     }
  74.     try {
  75.         if (!LastFm || !LastFm._service || !LastFm._service.loveBan || !LastFm._strings) {
  76.             throw new Error("LastFm not installed!");
  77.         }
  78.         document.getElementById('MinTrayR_lastFM-love').setAttribute('label', LastFm._strings.getString('lastfm.faceplate.love.tooltip'));
  79.         document.getElementById('MinTrayR_lastFM-ban').setAttribute('label', LastFm._strings.getString('lastfm.faceplate.ban.tooltip'));
  80.         this.show('MinTrayR_lastFM-sep', 'MinTrayR_lastFM-love', 'MinTrayR_lastFM-ban');
  81.     }
  82.     catch (ex) {
  83.         this.hide('MinTrayR_lastFM-sep', 'MinTrayR_lastFM-love', 'MinTrayR_lastFM-ban');
  84.     }
  85.     this._sp = {};
  86.     Components.utils.import('resource://app/jsmodules/sbProperties.jsm', this._sp);
  87.     this._sp = this._sp.SBProperties;
  88.     this._mm.addListener(this);
  89.     
  90.     this._titleTimer = new MinTrayR_CallbackTimer(this, 300, 0x1);
  91. }
  92. MinTrayRSongbird.prototype = {
  93.     _sp: null,
  94.     _mm: Components.classes["@songbirdnest.com/Songbird/Mediacore/Manager;1"]
  95.         .getService(Components.interfaces.sbIMediacoreManager),
  96.     evt: Components.interfaces.sbIMediacoreEvent,
  97.     
  98.     _mayFloat: true,
  99.     
  100.     _title: '',
  101.     _floatTitle: '',
  102.     get windowTitle() {
  103.         if (!this._title) {
  104.             return document.title;
  105.         }
  106.          return (this._mayFloat ? this._floatTitle : this._title) + ' - Songbird';
  107.     },
  108.     
  109.     unload: function(evt) {
  110.         this.log('unload');
  111.         if (this._titleTimer) {
  112.             this._titleTimer.cancel();
  113.             delete this._titleTimer;
  114.         }
  115.         return true;
  116.     },
  117.     onMediacoreEvent: function(evt) {
  118.         if (evt.type == this.evt.TRACK_CHANGE) {
  119.             let item = evt.data;
  120.             let sp = this._sp;
  121.             let rs = this.prefs
  122.                 .getExt('songbird.titlestring', '%trackName% / %artistName%')
  123.                 .replace(
  124.                     /%[\w\d]+?%/g,
  125.                     function(data) {
  126.                         let rv = '';
  127.                         try {
  128.                             rv = item.getProperty(sp[data.substr(1, data.length - 2)]);
  129.                             rv = rv || '';
  130.                         }
  131.                         catch (ex) {
  132.                             // no-op
  133.                         }
  134.                         return rv;
  135.                     }
  136.                 );
  137.             this._title = rs;
  138.             this._floatTitle = rs + "   ";
  139.             document.title = this.windowTitle;
  140.         }
  141.     },
  142.     
  143.     notify: function(subject, topic, data) {
  144.         if (!this._title) {
  145.             return;
  146.         }
  147.         if (this.prefs.getExt('songbird.titlefloating', false)) {
  148.             this._floatTitle = this._floatTitle.substr(1) + this._floatTitle[0];
  149.         }
  150.         document.title = this.windowTitle;
  151.     },
  152.         
  153.     loveBan_LastFm: function(loved) {
  154.         // unlove/unban
  155.         if (LastFm._service.loveTrack && LastFm._service.love == loved) {
  156.             LastFm._service.loveBan(null, false);
  157.         }
  158.         else {
  159.             LastFm._service.loveBan(gMM.sequencer.currentItem.guid, loved);
  160.         }
  161.     },
  162.     closeClicked: function() {
  163.         if ((this.prefs.getExt('minimizeon', 1) & (1 << 1)) != 0) {
  164.             this.minimize();
  165.             return true;
  166.         }
  167.         return false;
  168.     }
  169. };
  170.  
  171. var gMinTrayR;
  172. addEventListener(
  173.     'load',
  174.     function() gMinTrayR = new MinTrayRSongbird(),
  175.     true
  176. );
  177. addEventListener(
  178.     'unload',
  179.     function(evt) gMinTrayR && gMinTrayR.unload(evt),
  180.     false
  181. );
  182.  
  183. /* Songbird workaround
  184.  * SB does not issue standard OS messages for the close button,
  185.  * but instead implements it's own bindings and calls the close
  186.  * function directly.
  187.  */
  188. if ('quitApp' in this) {
  189.     quitApp = (function() {
  190.         let _p = quitApp;
  191.         return function() {
  192.             try {
  193.                 // examine the stack; if we're called from the sys buttons binding then 
  194.                 // see if we should minimize instead
  195.                 let stack = (new Error()).stack.toString();
  196.                 if (
  197.                     stack.indexOf('chrome://songbird/content/bindings/sysControls.xml') != -1
  198.                     && gMinTrayR.closeClicked() 
  199.                 ) {
  200.                     return null;
  201.                 }
  202.             }
  203.             catch (ex) {
  204.                 gMinTrayR.log(ex);
  205.             }
  206.             return _p.apply(this, arguments);            
  207.         }
  208.     })();
  209. }